package com.airbnb.mvrx.sample.features.book.models

import android.animation.ArgbEvaluator
import android.graphics.Color
import android.support.v4.app.FragmentActivity
import com.airbnb.mvrx.BaseMvRxViewModel
import com.airbnb.mvrx.MvRxViewModelFactory
import com.airbnb.mvrx.sample.Utils
import com.airbnb.mvrx.sample.core.MvRxViewModel
import com.airbnb.mvrx.sample.core.UpApplication
import com.airbnb.mvrx.sample.models.RespState
import com.airbnb.mvrx.sample.network.BooklnService
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.BookHomeDetailDTO
import org.json.JSONObject
import org.koin.android.ext.android.inject

public class BookDetailViewModel(initialState: RespState, private val booklnService: BooklnService) : MvRxViewModel<RespState>(initialState) {

    private val HEADER_BG_HEIGHT by lazy { Utils.dp2px(UpApplication.getApp(), 189) }
    private var mArgbEvaluator: ArgbEvaluator? = null

    init {
        mArgbEvaluator = ArgbEvaluator()
        fetchBookDetail()
    }


    private fun fetchBookDetail() = withState {
        val data: String = JSONObject().put("bookId", 26842).toString()
        val map: MutableMap<String, Any> = mutableMapOf()
        map.put("_data", data)
        map.putAll(Utils.getCommonParam())
        booklnService.fetchBookDetail(map).execute { copy(resp = it) }
    }

    fun getNewArgbColor(scrollY: Int, paletteColor: Int): Int {
        var fraction = scrollY * 1f / HEADER_BG_HEIGHT
        fraction = Math.min(fraction, 1.0f)
        val gradientColor = mArgbEvaluator?.evaluate(fraction, Color.WHITE, paletteColor) as Int
        return Color.argb(Math.min(Math.max(scrollY * 255 / HEADER_BG_HEIGHT, 0), 255), Color.red(gradientColor),
                Color.green(gradientColor), Color.blue(gradientColor))
    }

    fun getTitleShowPosition(): Int {
        return HEADER_BG_HEIGHT + Utils.dp2px(UpApplication.getApp(), 5)
    }


    fun getFuncList(bookDetailDTO: BookHomeDetailDTO): List<FuncGrid> {
        val funcList: MutableList<FuncGrid> = mutableListOf()
        if (bookDetailDTO.bookDTO != null) {
            funcList.add(FuncGrid(R.mipmap.ic_library_resources, "图书资源",
                    bookDetailDTO.bookDTO.authType, bookDetailDTO.bookDTO.authVal))
        }
        if (bookDetailDTO.clickRead != null) {
            funcList.add(FuncGrid(R.mipmap.ic_some_reading, "点读书",
                    bookDetailDTO.clickRead.authType, bookDetailDTO.clickRead.authVal))
        }
        if (bookDetailDTO.bookDTO.buyUrl != null && bookDetailDTO.bookDTO.buyUrl.startsWith("http")) {
            funcList.add(FuncGrid(R.mipmap.ic_bookcase_paperbooks, "购买纸质书", -1, ""))
        }
        return funcList
    }

    companion object : MvRxViewModelFactory<RespState> {
        @JvmStatic
        override fun create(activity: FragmentActivity, state: RespState)
                : BaseMvRxViewModel<RespState> {
            val service: BooklnService by activity.inject()
            return BookDetailViewModel(state, service)
        }
    }
}